//
//  FunctionMenuView.swift
//  letusmeet_ios
//
//  Created by Rovo Dev on 2025/1/27.
//

import SwiftUI

struct FunctionMenuView: View {
    @State private var selectedMenuItem: String? = nil
    
    private let menuItems = [
        MenuItem(icon: "👤", title: "个人资料", action: "profile"),
        MenuItem(icon: "⚙️", title: "账户设置", action: "settings"),
        MenuItem(icon: "🔒", title: "隐私设置", action: "privacy"),
        MenuItem(icon: "🔔", title: "消息通知", action: "notifications")
    ]
    
    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            // 标题
            HStack {
                Text("⚙️")
                    .font(.system(size: 18))
                Text("功能设置")
                    .font(.system(size: 18, weight: .semibold))
                    .foregroundColor(.primary)
            }
            .padding(.horizontal, 16)
            
            // 菜单列表
            VStack(spacing: 0) {
                ForEach(Array(menuItems.enumerated()), id: \.element.id) { index, item in
                    MenuItemRow(
                        item: item,
                        isSelected: selectedMenuItem == item.action,
                        showDivider: index < menuItems.count - 1
                    ) {
                        handleMenuTap(item.action)
                    }
                }
            }
            .background(Color(NSColor.controlBackgroundColor))
            .clipShape(RoundedRectangle(cornerRadius: 12))
            .shadow(color: .black.opacity(0.05), radius: 8, x: 0, y: 2)
        }
    }
    
    private func handleMenuTap(_ action: String) {
        withAnimation(.easeInOut(duration: 0.2)) {
            selectedMenuItem = action
        }
        
        // 模拟跳转延迟
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            selectedMenuItem = nil
            
            // 这里可以添加实际的导航逻辑
            switch action {
            case "profile":
                print("跳转到个人资料页面")
            case "settings":
                print("跳转到账户设置页面")
            case "privacy":
                print("跳转到隐私设置页面")
            case "notifications":
                print("跳转到消息通知页面")
            default:
                break
            }
        }
    }
}

struct MenuItem {
    let id = UUID()
    let icon: String
    let title: String
    let action: String
}

struct MenuItemRow: View {
    let item: MenuItem
    let isSelected: Bool
    let showDivider: Bool
    let onTap: () -> Void
    
    var body: some View {
        VStack(spacing: 0) {
            Button(action: onTap) {
                HStack(spacing: 16) {
                    // 图标
                    Text(item.icon)
                        .font(.system(size: 20))
                        .frame(width: 24, height: 24)
                    
                    // 标题
                    Text(item.title)
                        .font(.system(size: 16, weight: .medium))
                        .foregroundColor(.primary)
                    
                    Spacer()
                    
                    // 箭头
                    Image(systemName: "chevron.right")
                        .font(.system(size: 14, weight: .medium))
                        .foregroundColor(.secondary)
                }
                .padding(.horizontal, 16)
                .padding(.vertical, 16)
                .background(isSelected ? Color(NSColor.controlAccentColor).opacity(0.1) : Color.clear)
                .scaleEffect(isSelected ? 0.98 : 1.0)
            }
            .buttonStyle(PlainButtonStyle())
            
            if showDivider {
                Divider()
                    .padding(.leading, 56)
            }
        }
    }
}

#Preview {
    FunctionMenuView()
        .padding()
        .background(Color(NSColor.controlBackgroundColor))
}